import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.emos.workflow.EmosWorkflowApplication;
import com.example.emos.workflow.db.dao.TbUserDao;
import com.example.emos.workflow.service.WorkflowService;
import com.example.emos.workflow.task.SendEmailTask;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Yanx
 * @Create 2022-05-18-16:33
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = EmosWorkflowApplication.class)
public class test {

    @Autowired
    private TbUserDao userDao;


    @Autowired
    private TaskService taskService;

    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private HistoryService historyService;

    @Test
    public void contextLoads() {
        HashMap param = new HashMap();
        param.put("url", "www.baidu.com");
        param.put("uuid", "uuid");
        param.put("creatorId", "25");
        param.put("creatorName", "赵四");
        param.put("title", "赵四bug");
        param.put("date", "2022-5-18");
        param.put("start", "23:00");
        param.put("meetingType", "线上会议");
        param.put("managerId", "23");//部门经理
        param.put("gmId", "23");//总经理
        param.put("sameDept", "false");
        param.put("filing", false);
        param.put("type", "会议申请");
        param.put("createDate", DateUtil.today());
        param.put("identity", "员工");


        //部署
        String instanceId = runtimeService.startProcessInstanceByKey("meeting", param).getProcessInstanceId();
        //部署之后保存 instanceID 避免重复 部署取
        //String instanceId = "a6e6e99e-d6b8-11ec-94c7-665d86cd1a13";


        //获取流程实例参数
        HistoricTaskInstanceQuery taskQuery = historyService.createHistoricTaskInstanceQuery()
                .orderByHistoricTaskInstanceStartTime().desc()
                .includeTaskLocalVariables().includeProcessVariables()
                .processInstanceId(instanceId);
        HistoricTaskInstance historicTaskInstance1 = taskQuery.list().get(0);
        Map<String, Object> taskLocalVariables = historicTaskInstance1.getTaskLocalVariables();
        System.out.println(taskLocalVariables);

        //获取流程实例参数
        String id = taskService.createTaskQuery().processInstanceId(instanceId).singleResult().getId();
        Map<String, Object> variables = taskService.getVariables(id);

        String runtimeId = runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).singleResult().getId();
        HashMap map = (HashMap) runtimeService.getVariables(runtimeId);
        String identity = MapUtil.getStr(map,"identity");
        if(identity.equals("员工")){
            map.put("roleName", "部门经理");
            map.put("deptId", 2);
        }


        ArrayList<HashMap> arrayList = userDao.searchEmailByRoles(map);
        arrayList.forEach(one -> {
            System.out.println(one.get("email"));
            System.out.println(one);
        });

    }

}
